package LawsonTransformJava.xref;

import java.io.IOException;
import java.sql.SQLException;

public class XrefDedCode extends CrossRef {

	private final static String TABLE_NAME = "xDEDCODE";
	static final String XLATE_ERROR = "DCD_ERROR";
	static int taxBen = 0;
	static String old = null;
	static String newX = null;
	static String newHist = null;

	public XrefDedCode(boolean allowBlanks) throws SQLException, IOException {		
		stmt = xref.getDataStatement();
		rslt = stmt.executeQuery("SELECT * FROM " + TABLE_NAME);
		super.XLATE_ERROR=XLATE_ERROR;
		super.allowBlanks = allowBlanks;
		while (rslt.next()) {
			old = rslt.getString(2);			//Old
			newX = rslt.getString(4);			//New
			newHist = rslt.getString(6);		//Hist
			taxBen = rslt.getInt(7);			//Mapped Benefit(1)/Benefit(2)/Taxable(3)/Other(blank)
			//Map old code to CSV of New,History,Taxable-Benefit
			super.values.put(old, newX+","+newHist+","+taxBen);
		}
		
		super.close();
	}

	public String[] getValues(String lookup) {
		//Clear static values
		old = null;
		newX = null;
		newHist = null;
		taxBen = 0;
		//Split the mapped values by the comma to create an Array
		String chkLookup = xlate(lookup);
		if (!CrossRef.errFound && chkLookup.length() > 0) {
			old = lookup;
			newX = chkLookup.split(",")[0];
			newHist = chkLookup.split(",")[1];
			taxBen = Integer.parseInt(chkLookup.split(",")[2]);
			return chkLookup.split(",");
		}
		return null;
	}

	public static String getNew() {
		return newX;
	}
	
	public String lookupNew(String lookup) {
		String [] split = getValues(lookup);
		if (split != null && split.length>1)
			//Return New value
			return split[0];
		//Return ERROR
		return getError(lookup);
	}
	
	public static String getHist() {
		return newHist;
	}
	
	public String lookupHist(String lookup) {
		String [] split = getValues(lookup);
		if (split != null && split.length>1)
			//Return New value
			return split[1];
		//Return ERROR
		return getError(lookup);
	}
	
	public static int getTaxBen() {
		return taxBen;
	}
	
	public int lookupTaxBen(String lookup) {
		String [] split = getValues(lookup);
		if (split != null && split.length>1)
			//Return Taxable/Benefit
			return Integer.parseInt(split[2]);
		//Return ERROR
		return 0;
	}
	
	public static String getOld() {
		return old;
	}
	
	public String getError(String lookup) {
		return XLATE_ERROR + "_" + String.valueOf(stripQuotes(lookup));
	}
}
